package com.study.system;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class DanmuServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // HTTP 解码器
        pipeline.addLast("http-decoder", new HttpRequestDecoder());
        // HTTP 编码器
        pipeline.addLast("http-encoder", new HttpResponseEncoder());
        // 聚合HTTP请求
        pipeline.addLast("http-aggregator", new HttpObjectAggregator(65536));
        // 分块传输处理
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
        // WebSocket 协议处理器
        pipeline.addLast("websocket-protocol", new WebSocketServerProtocolHandler("/ws"));
        // 自定义业务逻辑处理器
        pipeline.addLast("websocket-handler", new DanmuServerHandler());
    }
}
